import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/base/base_controller.dart';
import 'package:ball_lion/module/expert/top/league_top_list_logic.dart';
import 'package:ball_lion/module/expert/top/top_list_logic.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/module/expert/top/top_list_view.dart';

class ExpertTopLogic extends BaseController with GetTickerProviderStateMixin {
  late final TabController controller;
  late final TabController zqController = TabController(length: 5, vsync: this);
  late final TabController lqController = TabController(length: 4, vsync: this);
  final ScrollController scrollController = ApmScrollController();
  Function()? onRefresh;
  int zqIndex = 0;
  int lqIndex = 0;
  int tabIndex = 0;
  String? onlySale = Get.parameters["onlySale"];
  final bool showAppBar;
  ExpertTopLogic({this.showAppBar = false});

  @override
  void onInit() {
    if (showAppBar) tabIndex = Get.arguments ?? 0;
    controller = TabController(length: 2, vsync: this, initialIndex: tabIndex);
    Utils.onEvent('fa_zjb', params: {"fa_zjb": tabIndex == 0 ? "足球" : "篮球"});
    controller.addListener(() {
      tabIndex = controller.index;
      Utils.onEvent('fa_zjb', params: {"fa_zjb": tabIndex == 0 ? "足球" : "篮球"});
      update();
    });
    zqController.addListener(() {
      zqIndex = zqController.index;
      update();
    });
    lqController.addListener(() {
      lqIndex = lqController.index;
      update();
    });
    int? initialPage = Get.parameters["pageIndex"].toInt();
    if (initialPage != null) {
      switch (tabIndex) {
        case 0:
          if (initialPage < 5) {
            zqController.animateTo(initialPage);
          }
          break;
        case 1:
          if (initialPage < 4) {
            lqController.animateTo(initialPage);
          }
          break;
      }
    }
    super.onInit();
  }

  doRefresh() {
    if (refreshController.headerState?.mode.isIdle == false) return;
    refreshController.callRefresh(scrollController: scrollController);
  }

  @override
  void getRefresh() {
    switch (tabIndex == 0 ? zqIndex : lqIndex) {
      case 0:
        Get.find<TopListLogic>(tag: "${tabIndex + 1}${TopType.lianhong}")
            .onRefresh();
        break;
      case 1:
        Get.find<TopListLogic>(tag: "${tabIndex + 1}${TopType.mingzhong}")
            .onRefresh();
        break;
      case 2:
        Get.find<TopListLogic>(tag: "${tabIndex + 1}${TopType.huibao}")
            .onRefresh();
        break;
      case 3:
        Get.find<LeagueTopListLogic>(tag: "${tabIndex + 1}").onRefresh();
    }
    update();
    super.getRefresh();
  }
}
